package com.test;

/**
 * Created by lenovo on 2017/3/29.
 * 产生4个线程，2个对i进行减操作，2个对i进行加操作
 */
public class FourThred {

    private int i=0;


    private synchronized void inc(){
        i++;
    }

    private synchronized  void dec(){
        i--;
    }
       class Inc implements Runnable {
        @Override
        public void run() {
            inc();
            System.out.println(i);
        }
    }

      class Dec implements  Runnable{
        @Override
        public void run() {
            dec();
            System.out.println(i);
        }
    }

    public  static void main(String[] args){

        FourThred ft = new FourThred();

        Inc inc = ft.new Inc();
        Dec dexc = ft.new Dec();
        while (true) {
            for (int j = 0; j < 2; j++) {
                Thread t1 = new Thread(inc);
                t1.start();
                t1 = new Thread(dexc);
                t1.start();
            }
        }
    }

}
